﻿using System.ComponentModel;
using System.Data;
using System.Data.SQLite;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace 树结构菜单
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window, INotifyPropertyChanged
    {
        private List<DataItem> list = new List<DataItem>();
        public MainWindow()
        {
            InitializeComponent();
            Random random = new Random();
            for (int i = 0; i < 1000; i++)
            {
                DataItem item = new DataItem()
                {
                    ID = i,
                    Name = "user:" + i,
                    RegDate = DateTime.Now.AddDays(-i),
                    Rate = random.NextSingle() * 100
                };
                list.Add(item);
            }
            lv.ItemsSource = list;

            this.DataContext = this;
        }

        private string initPass;

        public string InitPass
        {
            get { return initPass; }
            set
            {
                initPass = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(InitPass)));
            }
        }

        private string originPass;

        public string OriginPass
        {
            get { return originPass; }
            set
            {
                originPass = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(OriginPass)));
            }
        }

        private string newPass;

        public string NewPass
        {
            get { return newPass; }
            set
            {
                newPass = value;
                PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(nameof(NewPass)));
            }
        }



        public ICommand ConnectCmd
        {
            get => new BaseCommand(Connect);
        }

        public ICommand CreateDbCmd
        {
            get => new BaseCommand(CreateDb);
        }

        public event PropertyChangedEventHandler? PropertyChanged;

        private void Connect(object arg)
        {
            // 没有证书使用免费版的话这个命令就无法使用
            string connStr = "Data Source=C:\\Users\\32371\\Desktop\\temp.db;";

            using (SQLiteConnection conn = new SQLiteConnection(connStr))
            {
                conn.Open();
                SQLiteCommand command = conn.CreateCommand();
                command.CommandText = "select * from User";
                
            }
        }

        private void CreateDb(object obj)
        {
            string dbFile = "temp.db";
            SQLiteConnection.CreateFile(dbFile);

            using (SQLiteConnection conn = new SQLiteConnection($"Data Source={dbFile};"))
            { 
                conn.Open();
                SQLiteCommand cmd = conn.CreateCommand();
                cmd.CommandText = @"
                    CREATE TABLE IF NOT EXISTS User (
                        Id INTEGER PRIMARY KEY AUTOINCREMENT,
                        Name TEXT NOT NULL,
                        Age INTEGER
                    )";
                cmd.ExecuteNonQuery();
            }
        }
    }

    public class BaseCommand : ICommand
    {
        public BaseCommand(Action<object> executeAction)
        {
            ExecuteAction = executeAction;
        }

        public event EventHandler? CanExecuteChanged;

        public Action<object> ExecuteAction { get; set; }

        public bool CanExecute(object? parameter)
        {
            return true;
        }

        public void Execute(object? parameter)
        {
            ExecuteAction.Invoke(parameter);
        }
    }

    public class User
    {
        public int Id { get; set; }
        public string Name { get; set; }
        public int Age { get; set; }
    }

    public class DataItem
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public float Rate { get; set; }
        public DateTime RegDate { get; set; }
    }
}